package com.android.settings.audio.peq.peq.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.widget.TextView;

import com.android.settings.audio.peq.util.BuildConfig;

/**
 * Created by We on 2023/4/3
 */
public class IndTextView extends TextView {
    private static final int SIZE_P = 5;
    private Paint paint = new Paint();

    private boolean indEnable;

    public IndTextView(Context context) {
        this(context, null);
    }

    public IndTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private final Path pathLeft = new Path();
    private final Path pathRight = new Path();
    private final Path pathCenter = new Path();
    private int selectColor;
    private int textWidth = 140;

    private int arcControlX = 30;//值越大，弧度越大
    private int arcControlY = 3;
    private int arcWidth = 50;//曲线的宽度

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.textWidth = w - arcWidth;
    }

    @Override
    public void draw(Canvas canvas) {
        if (tabBgStyle == TabBgStyle.LEFT) {
            pathLeft.reset();
            pathLeft.lineTo(textWidth, 0);
            pathLeft.cubicTo(textWidth + arcControlX, arcControlY, textWidth + arcWidth - arcControlX, getHeight() - arcControlY, textWidth + arcWidth, getHeight());
            pathLeft.lineTo(0, getHeight());
            pathLeft.lineTo(0, 0);
            paint.setColor(selectColor);
            canvas.drawPath(pathLeft, paint);
        } else if (tabBgStyle == TabBgStyle.RIGHT) {
            pathRight.reset();
            pathRight.moveTo(getWidth(), 0);
            pathRight.lineTo(getWidth() - textWidth, 0);
            pathRight.cubicTo(getWidth() - textWidth - arcControlX, arcControlY, getWidth() - textWidth - arcWidth + arcControlX, getHeight() - arcControlY, getWidth() - textWidth - arcWidth, getHeight());
            pathRight.lineTo(getWidth(), getHeight());
            pathRight.lineTo(getWidth(), 0);
            paint.setColor(selectColor);
            canvas.drawPath(pathRight, paint);
        } else if (tabBgStyle == TabBgStyle.MIDDLE) {
            pathCenter.reset();
            pathCenter.moveTo(0, getHeight());//左下角起始点
            pathCenter.cubicTo(getWidth() / 8f, getHeight() - 10/*x1,y1底部左侧控制点1*/, 0f, 10f/*x2,y2顶部左侧控制点2*/, getWidth() / 5f, 0/*x3,y3顶部左侧结束点3*/);
            pathCenter.lineTo(getWidth() - (getWidth() / 5f), 0);/*顶部右侧起始点*/
            pathCenter.cubicTo(getWidth(), 10/*x1,y1顶部右侧控制点1*/, getWidth() - (getWidth() / 8f), getHeight() - 10/*x2,y2底部右侧控制点2*/, getWidth(), getHeight()/*x3,y3底部右侧结束点3*/);
            pathCenter.lineTo(0, getHeight());//左下角
            paint.setColor(selectColor);
            canvas.drawPath(pathCenter, paint);
        } else if (tabBgStyle == TabBgStyle.NEW_THEME) {

        }
        super.draw(canvas);
        if (indEnable) {
            if (!BuildConfig.isNewTheme()) {
                paint.setColor(Color.GREEN);
                canvas.drawCircle((getWidth() - SIZE_P) / 2f, getHeight() - SIZE_P, SIZE_P, paint);
            } else {
                paint.setColor(Color.parseColor("#FF7300"));
                //在左上角绘制一个直角三角形
                Path trianglePath = new Path();
                trianglePath.moveTo(0, 0); // Start at the top-left corner
                trianglePath.lineTo(30, 0); // Draw line to the right
                trianglePath.lineTo(0, 30); // Draw line downwards
                trianglePath.close(); // Close the path to form a triangle
                canvas.drawPath(trianglePath, paint);
            }
        }
    }

    public void setIndEnable(boolean indEnable) {
        this.indEnable = indEnable;
        invalidate();
    }


    private TabBgStyle tabBgStyle;

    public void setTabStyle(TabBgStyle tabBgStyle) {
        this.tabBgStyle = tabBgStyle;
        invalidate();
    }

    public enum TabBgStyle {
        LEFT,
        MIDDLE,
        RIGHT,
        NONE,
        NEW_THEME
    }

    public void setSelectColor(int color) {
        this.selectColor = color;
    }
}
